<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: liustone <905974667@qq.com>
// +----------------------------------------------------------------------
namespace Think\Template\TagLib;
use Think\Template\TagLib;
/**
 * Stone自定义标签库驱动
 */
class St extends TagLib{
    

    // protected $tags 定义调用标签
    protected $tags = array(
        // '标签名'=>array('attr'=>'传入参数变量,传入参数变量','close'=>1) //close 是否闭合（0 或者1 默认为1，表示闭合）
        'article'=> array('attr'=> 'cate_id,kw,limit,order,map','close'=>1),
		'category'=> array('attr'=> 'pid,kw,limit,order,map','close'=>1),
        'shopinfo'=> array('attr'=> 'name,shopid,fun','close'=>0),
    );
    
    //列表类自定义标签
    public function _article($attr,$content){
        $attr = $attr;  //必须有，读取模板传入参数
        $id = I('id');
		echo 'id'.$id;
        //逻辑处理
        //接收各个参数分配到变量
        $cate_id   = $attr['cate_id'];
        $kw     = $attr['kw'];
		$limit = $attr['limit'];
		$order = $attr['order'];
		$map['_string'] = $attr['map'];
        
        if ($cate_id) {
            $id_arr = D('ArticleCate')->get_child_ids($cate_id, true);
            $map['cate_id'] = array('IN', $id_arr);
        }
		($keyword = $kw) && $map['title'] = array('like', '%'.$keyword.'%');
        
        //得到数据
        $list = M("Article")->field('id,title,cate_id,add_time,img')->where($map)->limit($limit)->order($order)->select();
        $str = TagLib_list($content,$list); //模板数据处理
        return $str; //输出
    }
	
	//
	//列表类自定义标签
    public function _category($attr,$content){
        $attr = $attr;  //必须有，读取模板传入参数
        
        //逻辑处理
        //接收各个参数分配到变量
        $pid   = $attr['pid'];
        $kw     = $attr['kw'];
        
        if ($pid) {
            $id_arr = D('ArticleCate')->get_child_ids($cate_id, true);
            $map['id'] = array('IN', $id_arr);
        }
		($keyword = $kw) && $map['title'] = array('like', '%'.$keyword.'%');
        
        //得到数据
        $list = M("ArticleCate")->field('id,name,pid,alias,img,url')->where($map)->limit($limit)->order($order)->select();
        $str = TagLib_list($content,$list); //模板数据处理
        return $str; //输出
    }
    
    //根据传入值输出某个字段
    public function _shopinfo($attr,$content){
        $attr = $this->parseXmlAttr($attr);
        
        $shop_id = $_GET['shop_id'];
        $shop_id = ($attr['shopid'])?$attr['shopid']:$shop_id;
        $key      = $attr['name'];
        $fun      = $attr['fun'];
        $info      = M('shop')->table('bao_shop a,bao_shop_details b')->where('a.shop_id='.$shop_id.' AND b.shop_id='.$shop_id)->find();
        
        $str =TagLib_info($info[$key],$fun); //模板数据处理
        return $str;
    }
}